{include file="public/_meta" /}
<link rel="stylesheet" href="__STATIC__/manage/lib/jstree/dist/themes/default/style.css">
<body>
<div class="wrapper">

    {include file="public/_header" /}

    <!-- Sidebar -->
    {include file="public/_menu" /}
    <!-- End Sidebar -->

    <div class="main-panel">

        <div class="content">
            <div class="page-inner">

                <!-- 面包屑 -->
                {include file="public/_page_header" /}

                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="d-flex align-items-center">
                                    <h4 class="card-title">角色列表</h4>
                                    <button class="btn btn-primary btn-round ml-auto" data-toggle="modal" data-target="#addRowModal" onClick="add_item(0)">
                                        <i class="fa fa-plus"></i>
                                        添加
                                    </button>
                                </div>
                            </div>
                            <div class="card-body">
                                <!-- Modal -->
                                <div class="modal fade" id="addRowModal" tabindex="-1" role="dialog" aria-hidden="true">
                                    <div class="modal-dialog" role="document">
                                        <div class="modal-content">
                                            <div class="modal-header no-bd">
                                                <h5 class="modal-title">
                                                    <span class="fw-mediumbold">添加</span>

                                                </h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <form id="form-modal">
                                                    <div class="row">

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="title" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">角色名称</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <input type="text" class="form-control input-full" id="title" name="title" placeholder="角色名称">
                                                            </div>
                                                        </div>

                                                        <div class="form-group form-inline col-md-12">

                                                            <label for="title" class="col-md-3 col-form-label">权限管理</label>
                                                            <div class="col-sm-8 p-0">
                                                                <span class="text-muted col-sm-8"><input name="" id="checkall" type="checkbox" value="1"> <label style="display: inline;" for="checkall">选中全部</label></span>
                                                                <span class="text-muted col-sm-8" style="margin-left: 10%;"><input name="" id="expandall" type="checkbox" value="2"> <label style="display: inline;" for="expandall">展开全部</label></span>
                                                                <div id="treeview" style="margin-top: 20px;"></div>
                                                            </div>

                                                        </div>


                                                    </div>
                                                    <div class="modal-footer no-bd">
                                                        <input type="hidden" name="id" value="">
                                                        <button type="submit" id="addRowButton" class="btn btn-primary">保存</button>
                                                        <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
                                                    </div>
                                                </form>
                                            </div>

                                        </div>
                                    </div>
                                </div>

                                <div class="table-responsive">
                                    <table id="add-row" class="display table table-striped table-hover" >
                                        <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>角色名称</th>
                                            <th>时间</th>
                                            <th>是否启用</th>
                                            <th style="width: 10%">操作</th>
                                        </tr>
                                        </thead>

                                        <tbody>

                                        {volist name="authGroup" id="vo"}
                                        <tr>
                                            <td>{$vo.id}</td>
                                            <td>{$vo.title}</td>
                                            <td>{$vo.create_time}</td>
                                            <td>{$vo.status|getStatus}</td>
                                            <td>
                                                {if condition="$vo.id neq 1"}
                                                <div class="form-button-action">


                                                    {if condition="$vo.status eq 1"}

                                                        <button type="button" data-toggle="tooltip" title="" onClick="admin_stop(this,'{$vo.id}')" class="btn btn-link btn-warning" data-original-title="停用">
                                                            <i class="fa fa-toggle-on"></i>停用
                                                        </button>

                                                    {else/}

                                                    <button type="button" data-toggle="tooltip" title="" onClick="admin_start(this,'{$vo.id}')" class="btn btn-link btn-success" data-original-title="启用">
                                                        <i class="fa fa-toggle-off"></i>启用
                                                    </button>
                                                    {/if}

                                                    <button type="button" title="编辑" class="btn btn-link btn-primary btn-lg" data-toggle="modal" data-target="#addRowModal" onClick="edit_item('{$vo.id}')">
                                                        <i class="fa fa-edit"></i>编辑
                                                    </button>

                                                    <button type="button" data-toggle="tooltip" onClick="admin_del(this,'{$vo.id}')" title="" class="btn btn-link btn-danger" data-original-title="删除">
                                                        <i class="fa fa-times"></i>删除
                                                    </button>

                                                </div>
                                                {/if}


                                            </td>
                                        </tr>
                                        {/volist}

                                        </tbody>
                                    </table>

                                    <!-- 分页 (调取通用方法)-->
                                    {:pagination($authGroup)}

                                </div>
                            </div>
                        </div>
                    </div>
                </div>


            </div>
        </div>

        {include file="public/_footer" /}


    </div>


</div>

{include file="public/_js" /}

</body>
<script type="text/javascript" src="__STATIC__/manage/lib/jstree/dist/jstree.min.js"></script>
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">

    var SCOPE = {
        'status_url':"{:url('auth_group/status')}",
        'delete_url':"{:url('auth_group/delete')}",
        'save_url':"{:url('auth_group/save')}",
        'item_url':"{:url('auth_group/edit')}"
    };

    var group = {
        'rendertree': function (content) {
            $("#treeview")
                .on('redraw.jstree', function (e) {
                    $(".layer-footer").attr("domrefresh", Math.random());
                })
                .jstree({
                    "themes": {"stripes": true},
                    "checkbox": {
                        "keep_selected_style": false,
                    },
                    "types": {
                        "root": {
                            "icon": "fa fa-folder-open",
                        },
                        "menu": {
                            "icon": "fa fa-folder-open",
                        },
                        "file": {
                            "icon": "fa fa-file-o",
                        }
                    },
                    "plugins": ["checkbox", "types"],
                    "core": {
                        'check_callback': true,
                        "data": content
                    }
                });
        },
        'gettree': function (id) {
            $.ajax({
                url: "{:url('AuthGroup/roletree')}",
                type: 'get',
                dataType: 'json',
                data: {id: id},
                success: function (ret) {
                    if (ret.code == 1001) {
                        //销毁已有的节点树
                        $("#treeview").jstree("destroy");
                        group.rendertree(ret.data);
                    } else {
                        layer.msg(ret.msg,{icon:2,time:2000});
                    }
                }, error: function (e) {
                    layer.msg('网络错误',{icon:2,time:2000});
                }
            });
        },
    }

    $(function(){

        //全选和展开
        $(document).on("click", "#checkall", function () {
            $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
        });
        $(document).on("click", "#expandall", function () {
            $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
        });

        //读取选中的条目
        $.jstree.core.prototype.get_all_checked = function (full) {
            var obj = this.get_selected(), i, j;
            for (i = 0, j = obj.length; i < j; i++) {
                obj = obj.concat(this.get_node(obj[i]).parents);
            }
            obj = $.grep(obj, function (v, i, a) {
                return v != '#';
            });
            obj = obj.filter(function (itm, i, a) {
                return i == a.indexOf(itm);
            });
            return full ? $.map(obj, $.proxy(function (i) {
                return this.get_node(i);
            }, this)) : obj;
        };
        group.gettree(0);
        var commitStatus = false;
        /*表单验证*/
        $("#form-modal").validate({
            rules:{
                title:{
                    required:true,
                    minlength:2,
                    maxlength:10
                }
            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var url = SCOPE.save_url;
                var id = $("input[name='id']").val();
                var title = $("input[name='title']").val();
                var r = $("#treeview").jstree("get_all_checked");
                var rules = r.join(',');
                if(commitStatus==true){
                    return false;
                }
                /*表单提交*/
                $(form).ajaxSubmit({
                    type: 'post',
                    url: url,
                    data: {id:id,title:title,rules:rules},
                    success: function(data){
                        if(data.code == 1001){
                            commitStatus==true
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                parent.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg(errorThrown,{icon:2,time:2000});
                    }
                });

            }
        });
    });

    function add_item(id) {
        $("input[name='id']").val(id);
        document.getElementById('form-modal').reset();
        $(".fw-mediumbold").text('添加');
        group.gettree(0);
    }

    function edit_item(id) {
        $.ajax({
            type: 'GET',
            url: SCOPE.item_url,
            dataType: 'json',
            data: {id:id},
            success: function(res){
                if(res.code == 1001){
                    $(".fw-mediumbold").text('编辑');
                    $("input[name='id']").val(res.data.id);
                    $("input[name='title']").val(res.data.title);
                }else{
                    layer.msg('参数错误',{icon:2,time:2000});
                }

            },
            error:function(res) {
                layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
            }
        });
        group.gettree(id);
    }

    /*用户组-删除*/
    function admin_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
            $.ajax({
                type: 'POST',
                url: SCOPE.delete_url,
                dataType: 'json',
                data: {id:id},
                success: function(data){
                    console.log(data);
                    if(data.code == 1001){
                        $(obj).parents("tr").remove();
                        layer.msg('删除角色成功!',{icon:1,time:2000});
                    }else{
                        layer.msg(data.msg,{icon:2,time:2000});
                    }

                },
                error:function(data) {
                    layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
                }
            });
        });
    }

    /*用户组-停用*/
    function admin_stop(obj,id){
        layer.confirm('确认要停用吗？',function(index){
            //此处请求后台程序，下方是成功后的前台处理……
            var url = SCOPE.status_url;
            $.post(url,{status:2,id:id},function(data){
                if(data.code == 1001){
                    layer.msg('已停用!',{icon: 1,time:1000},function(){
                        window.location.reload();
                    });
                }else{
                    layer.msg(data.msg,{icon: 2,time:1000});
                }

            });


        });
    }

    /*用户组-启用*/
    function admin_start(obj,id){
        layer.confirm('确认要启用吗？',function(index){
            //此处请求后台程序，下方是成功后的前台处理……

            var url = SCOPE.status_url;
            $.post(url,{status:1,id:id},function(data){
                if(data.code == 1001){
                    layer.msg('已启用!',{icon: 1,time:1000},function(){
                        window.location.reload();
                    });
                }else{
                    layer.msg(data.msg,{icon: 2,time:1000});
                }
            });
        });
    }

</script>
</html>




























